package safari.controller.free;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import safari.model.common.ListVO;
import safari.model.free.FreeBoardService;
import safari.model.free.FreeBoardVO;
import safari.model.member.MemberVO;

public class FreeBoardController extends MultiActionController {
	private FreeBoardService fs;
	
	public FreeBoardController(FreeBoardService fs){
		this.fs = fs;
	}
	public String freeBoardGetAllList(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		ListVO fList = fs.freeBoardGetAllList(request.getParameter("page"));
		System.out.println(request.getParameter("page"));
		//System.out.println("일단 컨트롤러옴");
		request.setAttribute("freeboard", fList);
		return "free/list";
	}
	public String freeBoardShowContent(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		System.out.println("freeBoardShowContent : " + request.getParameter("no"));
		FreeBoardVO fbvo = fs.freeBoardShowContent(request.getParameter("no"));
		System.out.println("freeBoardShowContent : " + fbvo);
		request.setAttribute("freeboard", fbvo);
		return "free/showContent";
	}
	public String freeBoardRegisterView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		return "free/register";
	}
	public String freeBoardRegister(HttpServletRequest request, HttpServletResponse response, HttpSession session, FreeBoardVO paramFBVO) throws SQLException{
		//session에서 아이디와 객체를 저장하기 위한 준비
		MemberVO mvo = (MemberVO)session.getAttribute("userInfo");
		//paramFBVO에 아이디 주입
		paramFBVO.setId(mvo.getId());
		//paramFBVO에 객체 주입
//		paramFBVO.setMvo(mvo);
		// register 되면서 no를 주입받는다
		fs.freeBoardRegister(paramFBVO);
		System.out.println("freeBoardRegister : " + paramFBVO);
		return "redirect:/freeboard.do?command=freeBoardShowContent&no="+paramFBVO.getNo();
	}
	public String freeBoardupdateView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		System.out.println("freeBoardupdateView : " + no);
		FreeBoardVO fbvo = fs.freeBoardUpdateView(no);
		request.setAttribute("fbvo", fbvo);
		System.out.println("freeBoardupdateView : " + fbvo);
		return "free/updateView";
	} 
	public String freeBoardUpdate(HttpServletRequest request, HttpServletResponse response,HttpSession session, FreeBoardVO fbvo) throws SQLException{
		//System.out.println("freeBoardUpdate : " + fbvo);
		fs.freeBoardUpdate(fbvo);
		return "redirect:/freeboard.do?command=freeBoardShowContent&no="+fbvo.getNo();
	} 
	public String freeBoardDelete(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		//System.out.println("freeBoardDelete : " + request.getParameter("no"));
		int no = Integer.parseInt(request.getParameter("no"));
		fs.freeBoardDelete(no);
		return "redirect:/freeboard.do?command=freeBoardGetAllList";
	}
	
	public String freeBoardReplyView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		//System.out.println("freeBoardDelete : " + request.getParameter("no"));
		System.out.println("freeBoardReplyView : " + request.getParameter("no"));
		FreeBoardVO fbvo = fs.freeBoardReplyView(request.getParameter("no"));
		request.setAttribute("fbvo", fbvo);
		return "free/replyView";
	} 
	public String freeBoardReply(HttpServletRequest request, HttpServletResponse response, HttpSession session, FreeBoardVO paramFBVO) throws SQLException{
		//System.out.println("freeBoardDelete : " + request.getParameter("no"));
		System.out.println("freeBoardReply : " + paramFBVO);
		MemberVO mvo = (MemberVO)session.getAttribute("userInfo");
		paramFBVO.setId(mvo.getId());
		fs.freeBoardReplyRegister(paramFBVO);
		
		return "redirect:/freeboard.do?command=freeBoardGetAllList";
	}
}
